#!/usr/bin/bash

COMMAND=$1

function update_repo() {
    branch=$1
    cat >/etc/yum.repos.d/ci-tools.repo <<EOF
[$branch]
name=$branch
baseurl=http://repo.openeuler.org/openEuler-22.03-LTS/$branch/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://repo.openeuler.org/openEuler-22.03-LTS/$branch/$basearch/RPM-GPG-KEY-openEuler
EOF
    chmod 777 /etc/yum.repos.d/ci-tools.repo

}

function install_rpms() {
    # 安装归档的rpm
    if [ ! -d $2 ]; then
        echo "[INFO]Start installing the archive RPM package."
        params=()
        for ((i = 2; i <= $#; i++)); do
            eval j=\$$i
            params+=("$j")
        done
        packages=${params[@]}
    else
        packages=$2/*.rpms
    fi
    # dnf install -y $packages >install.logs
    echo "[INFO]The specified rpm package has been installed."
}

function download_binarys() {
    echo "Start download binarys"
    project=$2
    package=$3
    repository=$4
    arch=$5
    osc getbinaries $project $package $repository $arch
    if [ $? -ne 0 ]; then
        echo "[ERROR] Failed to download the RPM package generated by $package project $project."
        exit 1
    fi
    mv ./binaries/*rpm .
    echo "[INFO]The RPM package generated by the $project is successfully downloaded."
}

case $COMMAND in
update_repo)
    update_repo $2
    ;;
install_rpms)
    install_rpms $*
    ;;
download_binarys)
    download_binarys $2 $3 $4 $5
    ;;
*)
    echo 'command error'
    ;;
esac
